﻿using Teamwish.Domain.Models;
using Ninject.Modules;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Web;

namespace Teamwish.Domain.Common
{
    public class InjectMoudleForNinject : NinjectModule
    {
        /// <summary>
        /// 注册DI服务
        /// </summary>
        /// <param name="container"></param>
        private  void RegisterBLL()
        {
            Dictionary<string, Type> dictInterface = new Dictionary<string, Type>();
            Dictionary<string, Type> dictDAL = new Dictionary<string, Type>();
            Assembly currentAssembly = Assembly.GetExecutingAssembly();
            string dalSuffix = ".BLL";
            string interfaceSuffix = ".IBLL";

            //对比程序集里面的接口和具体的接口实现类，把它们分别放到不同的字典集合里
            foreach (Type objType in currentAssembly.GetTypes())
            {
                string defaultNamespace = objType.Namespace;
                if (objType.IsInterface && defaultNamespace.EndsWith(interfaceSuffix))
                {
                    if (!dictInterface.ContainsKey(objType.FullName))
                    {
                        dictInterface.Add(objType.FullName, objType);
                    }
                }
                else if (defaultNamespace.EndsWith(dalSuffix))
                {
                    if (!dictDAL.ContainsKey(objType.FullName))
                    {
                        dictDAL.Add(objType.FullName, objType);
                    }
                }
            }

            //根据注册的接口和接口实现集合，使用IOC容器进行注册
            foreach (string key in dictInterface.Keys)
            {
                Type interfaceType = dictInterface[key];
                foreach (string dalKey in dictDAL.Keys)
                {
                    Type dalType = dictDAL[dalKey];
                    if (interfaceType.IsAssignableFrom(dalType))//判断DAL是否实现了某接口
                    {
                        Bind(interfaceType).To(dalType);                  
                    }
                }
            }
        }
        public override void Load()
        {

            Bind<EfDbContext>().ToSelf().InScope(p=>HttpContext.Current);

            //RegisterBLL();
            //Bind<IUserinfoBLL>().To<UserinfoBLL>();
            //Bind<EfDbContext>().ToSelf().InScope(ctx => HttpContext.Current);
            //Bind<EntityContext>().ToSelf().InScope(ctx => HttpContext.Current);
            //Bind<IUnitOfWork>().To<NHUnitOfWork>().InSingletonScope();
            //Bind<IInitTableSchema>().To<NHInitTableSchema>().InSingletonScope();
            //Bind<IAccountRepository>().To<AccountRepository>().InSingletonScope();

            //Bind<IAdRepository>().To<AdRepository>().InSingletonScope();
            //Bind<IArticleRepository>().To<ArticleRepository>().InSingletonScope();
            //Bind<IAdDayReportRepository>().To<AdDayReportRepository>().InSingletonScope();
            //Bind<IDayTotalReportListForAdminRepository>().To<DayTotalReportListForAdminRepository>().InSingletonScope();
            //Bind<IAdDayReportForSiteRepository>().To<AdDayReportForSiteRepository>().InSingletonScope();
            //Bind<IAdDayReportForMarketRepository>().To<AdDayReportForMarketRepository>().InSingletonScope();
            //Bind<IAdDayReportForMediaRepository>().To<AdDayReportForMediaRepository>().InSingletonScope();
            //Bind<IAdDayReportForAdminRepository>().To<AdDayReportForAdminRepository>().InSingletonScope();
            //Bind<IAdDayReportForAdvRepository>().To<AdDayReportForAdvRepository>().InSingletonScope();
            //Bind<IPaymentRepository>().To<PaymentRepository>().InSingletonScope();
            //Bind<IWebSiteRepository>().To<WebSiteRepository>().InSingletonScope();
            //Bind<IAdPositionRepository>().To<AdPositionRepository>().InSingletonScope();
            //Bind<ICache>().To<MemoryCache>().InSingletonScope();
            //Bind<IVisitRecordRepository>().To<VisitRecodRepository>().InSingletonScope();
            //Bind<IAdDayReportItemRepository>().To<AdDayReportItemRepository>().InSingletonScope();
            //Bind<IWebDayReportItemRepository>().To<WebDayReportItemRepository>().InSingletonScope();
            //Bind<ISiteDayReportItemRepository>().To<SiteDayReportItemRepository>().InSingletonScope();
            //Bind<IAdvPaymentRepository>().To<AdvPaymentRepository>().InSingletonScope();

            //Bind<IYCScreenSizeRepository>().To<YCScreenSizeRepository>().InSingletonScope();
            //Bind<IIpRepeatRepository>().To<IpRepeatRepository>().InSingletonScope();
            //Bind<ISingleWidthRepository>().To<SingleWidthRepository>().InSingletonScope();
            //Bind<IActiveReportItemRepository>().To<ActiveReportItemRepository>().InSingletonScope();

            //Bind<IAdTaskRepository>().To<AdTaskRepository>().InSingletonScope();
            //Bind<IAdTaskDayReportRepository>().To<AdTaskDayReportRepository>().InSingletonScope();
        }
    }
}